package carapace

import (
	
	
	
	

	
	
	
	
	
	
)

func actionPath( []string,  bool) Action {
	return ActionCallback(func( Context) Action {
		if len(.Value) == 2 && util.HasVolumePrefix(.Value) {
			// TODO should be fixed in Abs or wherever this is happening
			return ActionValues(.Value + "/") // prevent `C:` -> `C:.`
		}

		,  := .Abs(.Value)
		if  != nil {
			return ActionMessage(.Error())
		}

		 := filepath.ToSlash(filepath.Dir(.Value))
		if  == "." {
			 = ""
		} else if !strings.HasSuffix(, "/") {
			 =  + "/"
		}

		 := filepath.ToSlash(filepath.Dir())
		,  := os.ReadDir()
		if  != nil {
			return ActionMessage(.Error())
		}

		 := !strings.HasSuffix(, "/") && strings.HasPrefix(filepath.Base(), ".")

		 := make([]string, 0, len()*2)
		for ,  := range  {
			if ! && strings.HasPrefix(.Name(), ".") {
				continue
			}

			,  := .Info()
			if  != nil {
				return ActionMessage(.Error())
			}
			 := false
			if ,  := filepath.EvalSymlinks( + "/" + .Name());  == nil {
				if ,  := os.Stat();  == nil {
					 = .IsDir()
				}
			}

			switch {
			case .IsDir():
				 = append(, +.Name()+"/", style.ForPath(filepath.Clean(+"/"+.Name()+"/"), ))
			case :
				 = append(, +.Name()+"/", style.ForPath(filepath.Clean(+"/"+.Name()), )) // TODO colorist not returning the symlink color
			case !:
				if len() == 0 {
					 = []string{""}
				}
				for ,  := range  {
					if strings.HasSuffix(.Name(), ) {
						 = append(, +.Name(), style.ForPath(filepath.Clean(+"/"+.Name()), ))
						break
					}
				}
			}
		}
		if strings.HasPrefix(.Value, "./") {
			return ActionStyledValues(...).Invoke(Context{}).Prefix("./").ToA()
		}
		return ActionStyledValues(...)
	})
}

func actionFlags( *cobra.Command) Action {
	return ActionCallback(func( Context) Action {
		.InitDefaultHelpFlag()
		.InitDefaultVersionFlag()

		 := pflagfork.FlagSet{FlagSet: .Flags()}
		 := .IsShorthandSeries(.Value)

		 := make([]rune, 0)
		 := Batch()
		.VisitAll(func( *pflagfork.Flag) {
			switch {
			case .Hidden && !env.Hidden():
				return // skip hidden flags
			case .Deprecated != "":
				return // skip deprecated flags
			case .Changed && !.IsRepeatable():
				return // don't repeat flag
			case .IsMutuallyExclusive(.Flag):
				return // skip flag of group already set
			}

			if  {
				if .Shorthand != "" && .ShorthandDeprecated == "" {
					for ,  := range .Value[1:] {
						if  := .Flags().ShorthandLookup(string());  != nil && .Value.Type() != "bool" && .Value.Type() != "count" && .NoOptDefVal == "" {
							return // abort shorthand flag series if a previous one is not bool or count and requires an argument (no default value)
						}
					}
					 = append(, ActionStyledValuesDescribed(.Shorthand, .Usage, .Style()).Tag("shorthand flags").
						UidF(func( string,  uid.Context) (*url.URL, error) { return uid.Flag(, ), nil }))
					if .IsOptarg() {
						 = append(, []rune(.Shorthand)[0])
					}
				}
			} else {
				switch .Mode() {
				case pflagfork.NameAsShorthand:
					 = append(, ActionStyledValuesDescribed("-"+.Name, .Usage, .Style()).Tag("longhand flags").
						UidF(func( string,  uid.Context) (*url.URL, error) { return uid.Flag(, ), nil }))
				case pflagfork.Default:
					 = append(, ActionStyledValuesDescribed("--"+.Name, .Usage, .Style()).Tag("longhand flags").
						UidF(func( string,  uid.Context) (*url.URL, error) { return uid.Flag(, ), nil }))
				}

				if .Shorthand != "" && .ShorthandDeprecated == "" {
					 = append(, ActionStyledValuesDescribed("-"+.Shorthand, .Usage, .Style()).Tag("shorthand flags").
						UidF(func( string,  uid.Context) (*url.URL, error) { return uid.Flag(, ), nil }))
				}
			}
		})

		if  {
			if len() > 0 {
				return .ToA().Prefix(.Value).NoSpace(...)
			}
			return .ToA().Prefix(.Value)
		}
		return .ToA().MultiParts(".") // multiparts completion for flags grouped with `.`
	})
}

func initHelpCompletion( *cobra.Command) {
	, ,  := .Find([]string{"help"})
	if  != nil {
		return
	}

	if .Name() != "help" ||
		.Short != "Help about any command" ||
		!strings.HasPrefix(.Long, `Help provides help for any command in the application.`) {
		return
	}

	Gen().PositionalAnyCompletion(
		ActionCommands(),
	)
}